package 剑指Offer专项突破;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.concurrent.*;

/**
 * @Author zhang lei
 * @Date 2022-05-09 16:29
 */
public class Algo067 {
    public static void main(String[] args) throws InterruptedException {
//        System.out.println(findMaximumXOR(new int[]{3,10,5,25,2,8}));

        CountDownLatch cdl = new CountDownLatch(5);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
        final Integer leave = 5;

        ExecutorService es = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 5; i++) {
            es.submit(()->{
                try {
                    cyclicBarrier.await();
                    System.out.println(String.format("线程:%s, 时间:%s, 还有库存:%s", Thread.currentThread().getId(), LocalDateTime.now().toString(), leave));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                } finally {
                    cdl.countDown();
                }
            });
        }
        cdl.await();
        es.shutdown();

    }

    public static int findMaximumXOR(int[] nums) {
        if(nums.length == 1) {
            return 0;
        }
        int max = -1;
        for (int i = 0; i < nums.length-1; i++) {
            for (int j = i+1; j < nums.length; j++) {
                max = Math.max(max, nums[i]^nums[j]);
            }
        }
        return max;
    }
}
